Imports System.Data

Partial Class WebForms_Fund
    Inherits WebFormCommon

    Dim strPath As String

    Private Sub SetPageSecurity()
        Dim sec As New Secure
        Dim cfunc As New CIMSfunc

        sec.ParentPage = parentpage
        sec.SetPageAccess(Session("UserName"))

        If ViewState("mode") = "view" Then
            cfunc.FormDisable(divMain)
        End If

        cfunc = Nothing
        sec = Nothing

    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Allocation_txt.Attributes.Add("onkeypress", "return checkDecimal(this);")

        Select Case Me.PageControlMode
            Case ControlMode.NewBusiness
                strPath = "New_Business"
            Case ControlMode.ClientServicing
                strPath = "Client_Servicing"
        End Select

        If Not Page.IsPostBack Then
            Dim dao As New DAOMain.CommonDAO
            Dim lfunc As New PolyFunc.ASPFunc

            'Dim dt As DataTable = dao.ExecQuery("Select fund_cd, fund_desc From fund_mstr Order by fund_desc")

            Call InitVars()
            BindFundCmb()

            Policy_td.InnerText = ViewState("PolicyNo")
            Provider_td.InnerText = ViewState("Provider")
            Name_td.InnerText = ViewState("CName")
            Call SetPageSecurity()

            Call LoadFund()

            Dim sec As New Secure

            sec.ParentPage = ParentPage

            If IsNothing(ViewState("CID")) Then
                sec.AddControl(Save_btn, Secure.ControlTypes.AddType)
            Else
                sec.AddControl(Save_btn, Secure.ControlTypes.EditType)
            End If

            sec.SetPageAccess(Session("UserName"))
            sec = Nothing
        End If
    End Sub

    Private Sub BindFundCmb()
        Dim dao As New DAOAdmin.FUND_MSTR_DAO
        Dim vo As New DAOAdmin.FUND_MSTR_VO

        vo.PRDT_CD = GetProduct()
        Dim dt As DataTable = dao.Search(vo)
        Fund_cmb.DataSource = dt
        Fund_cmb.DataTextField = "fund_desc"
        Fund_cmb.DataValueField = "fund_cd"
        Fund_cmb.DataBind()

        Fund_cmb.Items.Insert(0, "")

        dao = Nothing
        vo = Nothing
    End Sub

    Private Function GetProduct() As String
        Dim dao As New DAOMain.POLICY_TRANS_DAO
        Dim vo As New DAOMain.POLICY_TRANS_VO
        Dim strParent As String = ""

        vo.POLICY_SEQ_CD = ViewState("Seq_CD")
        Dim dt As DataTable = dao.Search(vo)

        If dt.Rows.Count > 0 Then strParent = dt.Rows(0).Item("PRDT_CD")

        dao = Nothing
        vo = Nothing

        Return strParent
    End Function

    Private Sub LoadFund()
        If IsNothing(ViewState("CID")) Then Exit Sub

        Dim dao As New DAOMain.POLICY_FUND_DTL_DAO
        Dim vo As New DAOMain.POLICY_FUND_DTL_VO
        Dim cFunc As New CIMSfunc
        Dim LookUp As New LookUp.FillLookUp

        vo.FUND_CD = ViewState("CID")
        vo.POLICY_SEQ_CD = ViewState("Seq_CD")

        Dim dt As DataTable = dao.Search(vo)

        'TODO: Validate Allocation is not greater than 100%

        If dt.Rows.Count > 0 Then
            With dt.Rows(0)
                Fund_cmb.SelectedValue = .Item("FUND_CD")
                Fund_txt.Text = .Item("FUND_CD")
                Allocation_txt.Text = .Item("ALLOCATION")

                Fund_cmb.Enabled = False
                Fund_txt.Enabled = False
            End With
        End If

        LookUp = Nothing
        cFunc = Nothing
        dao = Nothing
        vo = Nothing
    End Sub

    Private Function GetFundTotals() As Double
        Dim dm As New DAOMain.CommonDAO
        Dim iReturn As Double

        dm.AddParameter("Fund")
        dm.AddParameter(ViewState("Seq_CD"))
        dm.AddParameter(Val(Allocation_txt.Text))
        If Not IsNothing(ViewState("CID")) Then
            dm.AddParameter(ViewState("CID"))
        Else
            dm.AddParameter("")
        End If
        dm.AddParameter("")

        Dim dt As DataTable = dm.ExecQryProc("sp_get_total_percent")

        If dt.Rows.Count > 0 Then
            iReturn = dt.Rows(0).Item("Percentage")
        End If

        Return iReturn
        dm = Nothing
    End Function

    Protected Sub Save_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Save_btn.Click
        Dim dao As New DAOMain.POLICY_FUND_DTL_DAO
        Dim vo As New DAOMain.POLICY_FUND_DTL_VO
        Dim cFunc As New CIMSfunc
        Dim polyFunc As New PolyFunc.ASPFunc

        If GetFundTotals() > 100 Then
            polyFunc.MessageAlert(parentpage, "Funds total percentage exceeds 100%")
            Exit Sub
        End If

        Dim voCheck As New DAOMain.POLICY_FUND_DTL_VO
        Dim daoCHeck As New DAOMain.POLICY_FUND_DTL_DAO

        Dim dt As DataTable

        With voCheck
            .POLICY_SEQ_CD = ViewState("Seq_CD")
            .FUND_CD = Fund_cmb.SelectedValue.ToString

            dt = daoCHeck.Search(voCheck)
        End With

        With vo
            .PRDT_CD = GetProduct()
            .ALLOCATION = Val(Allocation_txt.Text)
            .POLICY_SEQ_CD = ViewState("Seq_CD")

            Select Case dt.Rows.Count
                Case 0
                    .FUND_CD = Fund_cmb.SelectedValue
                    .POLICY_FUND_CREA_BY = Session("UserName")
                    .POLICY_FUND_CREA_DT = Now

                    If dao.Create_Record(vo) Then
                        InitSessions()
                        Response.Redirect("~/" & strPath & "/ClientInfo.aspx")
                    Else
                        polyFunc.MessageAlert(parentpage, "Error Saving Record!")
                    End If
                Case Else
                    If ViewState("CID") = Fund_txt.Text Then
                        .FUND_CD = Fund_cmb.SelectedValue
                        .POLICY_FUND_CREA_BY = Session("UserName")
                        .POLICY_FUND_CREA_DT = Now

                        If dao.Update(vo) Then
                            InitSessions()
                            Response.Redirect("~/" & strPath & "/ClientInfo.aspx")
                        Else
                            polyFunc.MessageAlert(ParentPage, "Error Saving Record!")
                        End If
                    Else
                        Dim pFunc As New PolyFunc.ASPFunc

                        pFunc.MessageAlert(ParentPage, "Fund already exists")
                        pFunc = Nothing
                    End If
            End Select
        End With

        polyFunc = Nothing
        cFunc = Nothing
        dao = Nothing
        vo = Nothing
    End Sub

    Protected Sub Cancel_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Cancel_btn.Click
        InitSessions()
        Response.Redirect("~/" & strPath & "/ClientInfo.aspx")
    End Sub

    Protected Sub Fund_cmb_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Fund_cmb.SelectedIndexChanged
        Fund_txt.Text = Fund_cmb.SelectedValue.ToString
    End Sub
End Class
